package fregat.usb.configprovider;


import java.io.FileNotFoundException;

import fregat.application.context.IApplicationContext;
import fregat.application.context.arguments.IApplicationArguments;
import fregat.base.util.Logger;
import fregat.usb.application.context.ApplicationContext;

public class ConfigProviderFactory implements IConfigProviderFactory {
	
	public IConfigProvider createConfigProvider (IApplicationContext context, ConfigProviderType cpt) throws UnknownProviderException {
		IConfigProvider result = null;
		switch (cpt) {
			case DATABASE :
				try {
					String configpath = (String)context.getApplicationArguments().findOption('c');
					if (configpath == null)
						return new DBConfigProvider ();
					return new DBConfigProvider(configpath);
				} catch (FileNotFoundException fnfex) {
					// TODO Auto-generated catch block
					Logger.getCategory("usb").error("", fnfex);
				}
			default :
				throw new UnknownProviderException ();
		}
	}
	
	public static void main (String [] args) {
		try {
			IApplicationContext appcontext = new ApplicationContext(args);
			ConfigProviderFactory cpf = new ConfigProviderFactory ();
			DBConfigProvider cp =  (DBConfigProvider) cpf.createConfigProvider(appcontext, ConfigProviderType.DATABASE);
			IConfigListener cl = new IConfigListener () {
				public void configChanged (IConfiguration c) {
					Logger.getCategory("usb").info("Alert received");
				}
			};
			cp.registerListener(cl);
			cp.startSession();
			
			cp.endSession();
			Logger.shutdownAll();
			System.exit(0);
			
		}
		catch(UnknownProviderException upex) {
			Logger.getCategory("usb").error("", upex);
		}
	}
}
